#ifndef __MINE_H_
#define __MINE_H_


#define uchar unsigned char
#define uint unsigned int

//数码管段选信号 从0到9和'-'
uchar code duanxuan[12]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40,0x00};
//数码管位选信号
uchar code weixuan[8]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07};

sbit led_sel=P2^3;//74HC138 的一个使能位
sbit key1=P3^2;//按键k1
sbit key2=P3^3;//按键k2
sbit key3=P1^7;//按键k2
sbit voice=P3^4;//蜂鸣器

uint number1;//显示在数码管上的值
uint number2;//显示在数码管上的值
uint flag;//位选的位置
uint count;
uint lcount;
int count1,count2,count3,count4;//用于消抖计数
bit status_P,status_P2;//记录按键前一状态
bit v;

void Init()
{
/**************************
将P0设置成推挽输出 P2的低四位设置成推挽输出 P3不需要设置成推挽输出
**************************/
	P0M0=0XFF;
	P0M1=0X00;
	P2M0=0X0F;
	P2M1=0X00;
	
	led_sel=0;
	P0=0;
	number1=0;//初始化number=5000
	number2=0;//初始化number=5000
	count=0;
	count1=0;
	count2=0;
	count3=0;
	count4=0;
	lcount=0;
	
	
	status_P=0;
	status_P2=0;
	flag=0;
	v=0;
	
	TMOD=0X00;
	EA=1;
	ET0=1;
	TH0=(65536-200)/256;
	TL0=(65536-200)%256;
	TR0=1;
}

void led(set)
{
	P0=0x00;
	led_sel=1;
	P0=set;
}

void voice_maker(time)
{
	led_sel=0;
	P0=0;
	if (count%time==0){
		v=~v;
	}	
	voice=v;
}

void light_display(flag,n01,n02,n03,n04,n05,n06,n07,n08,n11,n12,n13,n14,n15,n16,n17,n18)
{
	led_sel=0;
	P0=0x00;
	switch(flag)
	{	/*
		case 0:P0=0;P2=weixuan[flag];P0=duanxuan[number/1000];break;
		case 1:P0=0;P2=weixuan[flag];P0=duanxuan[(number/100)%10];break;
		case 2:P0=0;P2=weixuan[flag];P0=duanxuan[(number%100)/10];break;
		case 3:P0=0;P2=weixuan[flag];P0=duanxuan[(number%100)%10];break;
		*/
		case 0:P2=weixuan[0];P0=duanxuan[n01];break;
		case 1:P2=weixuan[1];P0=duanxuan[n02];break;
		case 2:P2=weixuan[2];P0=duanxuan[n03];break;
		case 3:P2=weixuan[3];P0=duanxuan[n04];break;
		case 4:P2=weixuan[4];P0=duanxuan[n05];break;
		case 5:P2=weixuan[5];P0=duanxuan[n06];break;
		case 6:P2=weixuan[6];P0=duanxuan[n07];break;
		case 7:P2=weixuan[7];P0=duanxuan[n08];break;
		case 8:P2=weixuan[0];P0=duanxuan[n11];break;
		case 9:P2=weixuan[1];P0=duanxuan[n12];break;
		case 10:P2=weixuan[2];P0=duanxuan[n13];break;
		case 11:P2=weixuan[3];P0=duanxuan[n14];break;
		case 12:P2=weixuan[4];P0=duanxuan[n15];break;
		case 13:P2=weixuan[5];P0=duanxuan[n16];break;
		case 14:P2=weixuan[6];P0=duanxuan[n17];break;
		case 15:P2=weixuan[7];P0=duanxuan[n18];break;
	}
}

void xiaodou_count()
{
	count1++;
	if(key1==0){
		count2++;
	}
	count3++;
	if(key3==0){
		count4++;
	}
}

void xiaodou_done(n1,n2,n3,n4)
{
	if(count1==n1)//消抖大概6ms 总共统计次数 30次
	{
		if(count2>=n2)// 如果低电平的有效次数为 2/3 以上
		{
			if(status_P==1)
			{
				status_P=0;
				number2++;
			}
		}
		else
			status_P=1;
		count1=0;
		count2=0;
	}
	if(count3==n3)//消抖大概6ms 总共统计次数 60次
	{
		if(count4>=n4)// 如果低电平的有效次数为 2/3 以上
		{
			if(status_P2==1)
			{
				status_P2=0;
				number1++;
			}
		}
		else
			status_P2=1;
		count3=0;
		count4=0;
	}
}

void end()
{
	count--;
	lcount++;
	if(number1>number2){
		if(lcount<512){
			light_display(flag,number1/100,(number1/10)%10,number1%10,10,10,number2/100,(number2/10)%10,number2%10,number1/100,(number1/10)%10,number1%10,10,10,11,11,11);
		}else if(lcount<1536){
			light_display(flag,11,11,11,10,10,number2/100,(number2/10)%10,number2%10,11,11,11,10,10,11,11,11);
		}else{
			lcount=0;
		}
	}else if(number1<number2){
		if(lcount<512){
			light_display(flag,number1/100,(number1/10)%10,number1%10,10,10,number2/100,(number2/10)%10,number2%10,11,11,11,10,10,number2/100,(number2/10)%10,number2%10);
		}else if(lcount<1536){
			light_display(flag,number1/100,(number1/10)%10,number1%10,10,10,11,11,11,11,11,11,10,10,11,11,11);
		}else{
			lcount=0;
		}		
	}else{
		if(lcount<512){
			light_display(flag,number1/100,(number1/10)%10,number1%10,10,10,number2/100,(number2/10)%10,number2%10,number1/100,(number1/10)%10,number1%10,10,10,number2/100,(number2/10)%10,number2%10);
		}else if(lcount<1536){
			light_display(flag,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11);
		}else{
			lcount=0;
		}		
	}
	flag++;		
}
#endif
